package com.yc.jpyy.penaltydoc.controller;

import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.penaltydoc.service.PenaltyDocService;
import com.yc.jpyy.utils.Tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

/**
 * 行政处罚档案管理
 * date: 2018-07-14
 * by:何创强
 */
@Controller
@RequestMapping(value = "/penaltyDoc")
@Scope("prototype")
public class PenaltyDocController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(PenaltyDocController.class);

    @Autowired
    private PenaltyDocService penaltyDocService;

    /**
     * 根据车牌号获取所有驾驶员信息
     * @param info
     * @return
     */
    @RequestMapping(value = "/getDriverCarRelationByVehicleNo", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getDriverCarRelationByVehicleNo(String info,HttpServletRequest request){
        logger.info("info",info);
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        try {
            Map<String,Object> paramsMap = Tools.JsonToMap(info);
            HandlerDataSource.setDataSource((String) paramsMap.get("dbcode"));
            List<Map<String, Object>> list = this.penaltyDocService
                    .getDriverCarRelationByVehicleNo((String) paramsMap.get("vehicleno"));
            response.setListData(list);
            if(list.size()>0) {
                response.setReturnMsg(1, "获取成功");
            }else{
                response.setReturnMsg(0, "获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setReturnException(e.toString());
            response.setReturnCode(0);
        }finally {
            HandlerDataSource.clear();
        }
        return  response.toJSON();
    }


    /**
     * 获取流水编号
     * 普货道路运输证号：130181000001至130181599999
     * 客车道路运输证号：130181600001至130181699999
     * 出租道路运输证号：130181700001至130181799999
     * 危货道路运输证号：130181800001至130181899999
     *
     * 1:登记编号；2:受理编号；3.许可编号；4.许可证号；
     * 5.普货道路运输证号；6：服务证号；7：卷宗号(货运)，
     * 8 客车道路运输证号；9 出租道路运输证号；
     * 10 危货道路运输证号
     * 11 网约车档案袋编号；12 巡游车档案袋编号； 13 固定资产编号；
     * 14 日常用品编号； 15 行政处罚档案； 16 默认编号
     * @return
     */
    @RequestMapping(value = "/getMaxNumByType", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getMaxNumByType(String info,HttpServletRequest request){
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        try {
            Map<String, Object> paramsMap = Tools.JsonToMap(info);
            //流水号类型
            //String serialNumberType = (String) paramsMap.get("serialnumbertype");
            //数据库编码
            String dbcode = (String) paramsMap.get("dbcode");
            HandlerDataSource.setDataSource(dbcode);
            String code = this.penaltyDocService.getMaxNumByType(paramsMap);
            response.setReturnMsg(1,code);
        }catch (Exception e){
            e.printStackTrace();
            response.setReturnException(e.getMessage());
            response.setReturnMsg(0,"获取失败");
        }finally {
            HandlerDataSource.clear();
        }
        return response.toJSON();
    }
    
}
